<?php
class Admin_AjaxController extends Clu_Controller_Action_Hvn
{
	public $mWeight;
	
	/*
	 * add new or remove a weight to a widget_weights table
	 */
    public function widgetWeightAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		
		$data['content_id'] = $this->_request->getParam('content_id');
		$data['content_title'] = $this->_request->getParam('content_title');
		$data['content_module'] = $this->_request->getParam('content_module');
		$data['widget_position_id'] = $this->_request->getParam('widget_position_id');
		if ($data['content_module'] == "node")
			$data['type'] = $this->_request->getParam('type');
		$operation = $this->_request->getParam('operation');
    	if($this->_request->isXmlHttpRequest())
		{
			if (strpos ($sess->userPermissions, 'position widget') === false)
			{
				$ret['success'] = false;
				$ret['errorMessage'] =  t(414);
				$this->_helper->json->sendJson($ret);
				exit();
			}
			
			//require_once (APP_PATH.'/modules/admin/Model/Weight.php');
			$this->mWeight = new admin_Model_Weight();
    	    $ret = array();
    	    if ($operation == 'insert')
    	    {
	            if ($this->mWeight->insertRaw($data))
	            {
	            	$ret['success'] = true;
	            }
	            else
	            {
	            	$ret['success'] = false;
	            	$ret['errorMessage'] = __("Could not insert");
	            }
    	    }
    	    elseif ($operation == 'delete')
    	    {
	            if ($this->mWeight->deleteRaw($data))
	            	$ret['success'] = true;
	            else
	            {
	            	$ret['success'] = false;
	            	$ret['errorMessage'] = __("Could not delete");
	            }
    	    }
			$this->_helper->json->sendJson($ret);
		}
	}
	
	/*
	 * $data = array of array 
	 * 	each row = 
	 * array ('content_id', 'content_module', widget_position_id, 'type'[optional]) 
	 * 	=> new weight,
	 * 
	 */
	public function updateMultipleWeightsAction()  
	{
		$order = $this->_request->getParam('order');
		$data = explode(',', $order);
		//require_once (APP_PATH.'/modules/admin/Model/Weight.php');
		$this->mWeight = new admin_Model_Weight();
		$ret = $this->mWeight->search(null);
		if($this->_request->isXmlHttpRequest())
		{
			foreach($data as $key => $value)
			{
				if(empty($value))
					unset($data[$key]);
			}
//			foreach($data as $key => $value)
//			{
//				//foreach($ret as  $k => $v)
//				{
//					//if($value == $v['content_id'])
//					{
//						$result['content_id'] = $value;
//						$result['weight'] = $key;
//					}
//				}
//			}
//			die_a($data);
			if($this->mWeight->updatedRaw($data))
			 	$ret['success'] = true;
			else
				$ret['success'] = false;
				
			$this->_helper->json->sendJson($ret);
		}
	}
	
	public function getDataWidgetPositionsAction()
	{
		$data = $this->_request->getParams();
		//require_once (APP_PATH.'/modules/admin/Model/Weight.php');
		$this->mWeight = new admin_Model_Weight();
		if($this->_request->isXmlHttpRequest())
		{
			$results = $this->mWeight->getDataWidgetPosition($data);
			$ret[] =  array('id' => 0,
			 		    	'name' => t(97));	
			foreach($results as $d)
			{
					$ret[] =  array(
                           		 /*'id' => $d['id'],*/
						 		 'name' => $d['master_request']
                   			);
			}
			$this->_helper->json->sendJson($ret);
		}
	}
	
	public function getWidgetNameAction()
	{
		//$masterRequest  = something like "hvn/master/home"
		$masterRequest = $this->_request->getParam('master_request');
		//require_once (APP_PATH.'/modules/admin/Model/Weight.php');
		$this->mWeight = new admin_Model_Weight();
		if($this->_request->isXmlHttpRequest())
		{
			$results = $this->mWeight->getWidgets($masterRequest);
			$ret[] =  array('id' => 0,
			 		    	'widget_name' => t(97));	
			foreach($results as $d)
			{
					$ret[] =  array(
                           		 'id' => $d['id'],
						 		 'widget_name' => ($d['widget_name'] != null) ? $d['widget_name'] : $d['widget_request'], 
						 		 'widget_request' => $d['widget_request'],
                   			);
			}
			$this->_helper->json->sendJson($ret);
		}
	}
}